/*项目所有请求接口*/
import Vue from 'vue';
import VueResource from 'vue-resource';
Vue.use(VueResource);
import { Message } from 'element-ui';

/*导入页面加载进度条*/
import NProgress from 'nprogress';

//通过CDN形式引入
import 'nprogress/nprogress.css';

// NProgress.configure({ease:'ease',speed:500});

/*全局配置请求跟路径*/
Vue.http.options.root='http://127.0.0.1:8888/api/private/v1/';
/*全局配置post请求表单数据格式组织形式*/
Vue.http.options.emulateJSON=true;

/*拦截器*/
Vue.http.interceptors.push(function(request, next) {
  // 请求发送前的处理逻辑
  /*添加token令牌*/
  NProgress.start();  // 开始加载进度条

  var token=window.sessionStorage.getItem('token');
  if(token){
    request.headers.set('Authorization',token);
  }
  next(function(response) {
    if(response.data.meta.status >= 200 && response.data.meta.status <300){
      NProgress.done();  // 加载完成
      return response;
    }
    Message.error('请求失败:'+response.data.meta.msg+' in '+response.url);
    throw Error(response);
  });
});

class MyInterface {
    constructor(){
      /*上传图片的url地址*/
      this.uploadUrl=Vue.http.options.root;
      this.uploadHeaders={Authorization:window.sessionStorage.getItem('token')}
    }
    login(userObj){
        /*登陆*/
        return Vue.http.post('login',userObj);
    }
    getMenus(){
      /*获取左侧菜单栏数据*/
      return Vue.http.get('menus');
    }
    getUserList(obj){
      /*获取用户数据列表*/
      return Vue.http.get('users',{params:obj});
    }
    updateUsersState(id,bool){
        /*修改用户状态*/
      return Vue.http.put(`users/${id}/state/${bool}`);
    }
    addUser(userObj){
      return Vue.http.post('users',userObj);
    }
    getUserById(id){
      /*根据id查询用户*/
      return Vue.http.get(`users/${id}`);
    }
    editUser(editFormObj){
      /*编辑用户操作*/
      return Vue.http.put(`users/${editFormObj.id}/`,editFormObj);
    }
    deleteUser(id){
      /*删除用户操作*/
      return Vue.http.delete(`users/${id}/`);
    }
    getRightsList(type='list'){
      /*获取所有权限列表type=tree*/
      return Vue.http.get(`rights/${type}`);
    }
    getRolesList(){
      /*获取角色列表*/
      return Vue.http.get(`roles/`);
    }
    deleteRolseRights(RolseId,RightsId){
        /*删除角色指定权限*/
      return Vue.http.delete(`roles/${RolseId}/rights/${RightsId}`);
    }
    roleAuthorization(roleId,arrId){
      /*角色授权*/
      return Vue.http.post(`roles/${roleId}/rights`,{'rids':arrId.toString()});
    }
    addRoles(RolesObj){
      /*添加角色*/
      return Vue.http.post('roles',RolesObj);
    }
    getRolesById(id){
      /*添加角色*/
      return Vue.http.get(`roles/${id}`);
    }
    editRoles(RolesObj){
      /*编辑角色*/
      return Vue.http.put(`roles/${RolesObj.roleId}/`,RolesObj);
    }
    deleteRolesById(id){
      /*删除角色*/
      return Vue.http.delete(`roles/${id}`);
    }
    setUserRole(userId,rolesId){
      /*分配用户角色*/
      return Vue.http.put(`users/${userId}/role`,{rid:rolesId});
    }
    getCateList(obj){
      /*获取商品分类数据列表*/
      return Vue.http.get('categories',{params:obj});
    }
    addCategories(CateObj){
      /*添加商品分类*/
      return Vue.http.post('categories',CateObj);
    }
    getCategoriesById(id){
      /*根据id 查询分类*/
      return Vue.http.get(`categories/${id}`);
    }
    editCategories(id,cat_name){
      /* 编辑提交分类*/
      return Vue.http.put(`categories/${id}`,{cat_name:cat_name});
    }
    deleteCategoriesByid(id){
      /*删除分类*/
      return Vue.http.delete(`categories/${id}`);
    }
    getCategories(id,sel='only'){
      /* 分类参数管理,参数列表*/
      return Vue.http.get(`categories/${id}/attributes`,{params:{'sel':sel}});
    }
    addParamsOrAttributes(obj){
      /*添加动态参数或者静态属性*/
      return Vue.http.post(`categories/${obj.id}/attributes`,obj);
    }
    getAttributesById(id,attrId){
      /*根据 ID 查询参数*/
      return Vue.http.get(`categories/${id}/attributes/${attrId}`);
    }
    editAttributes(obj){
      /*编辑提交参数*/
      return Vue.http.put(`categories/${obj.id}/attributes/${obj.attrId}`,obj);
    }
    deleteAttributesById(id,attrId){
      /*删除参数*/
      return Vue.http.delete(`categories/${id}/attributes/${attrId}`);
    }
    getGoodsList(obj){
      /*获取商品列表数据*/
      return Vue.http.get(`goods/`,{params:obj});
    }
    deleteGoodsById(id){
      /*根据id删除商品*/
      return Vue.http.delete(`goods/${id}`);
    }
    addGoods(goodsObj){
      /*添加商品*/
      return Vue.http.post('goods',goodsObj);
    }
    getOrders(obj){
      /*获取订单数据列表*/
      return Vue.http.get(`orders/`,{params:obj});
    }
    getKuaidiById(id='1106975712662'){
      /*根据订单编号查看物流信息*/
      return Vue.http.get(`kuaidi/${id}`);
    }
    getReports(){
      /*获取基于时间统计的折线图*/
      return Vue.http.get(`reports/type/1`);
    }

}
export default new MyInterface;
